import { mrcdetail, storedetail } from "@/api/mreinfo";
import { queryRegisterConfig } from "@/api/stores";

const common = {
  state: {
    Saved: false,
    AccordSaved: false,
    config: [],
    Autonomy: "",
    stepIndex: 0
  },
  mutations: {
    setStepIndex(state, data) {
      state.stepIndex = data;
    },
    Saved(state, data) {
      state.Saved = data;
    },
    AccordSaved(state, data) {
      state.AccordSaved = data;
    },
    Autonomy(state, data) {
      state.Autonomy = data;
    },
    setConfig(state, data) {
      state.config = data;
    },
  },
  actions: {
    SET_STEP_INDEX(context, data) {
      context.commit("setStepIndex", data);
    },
    setSaved(context, data) {
      context.commit("Saved", data);
    },
    setAccordSaved(context, data) {
      context.commit("AccordSaved", data);
    },
    Getmrcdetail({}, data) {
      return new Promise((resolve, reject) => {
        if (Number(data.type) === 1) {
          mrcdetail(data.id)
            .then((res) => {
              const data = res.data;
              resolve(data);
            })
            .catch((err) => {
              reject(err);
            });
        } else {
          storedetail(data.id)
            .then((res) => {
              const data = res.data;
              data.merchantFullName = data.storeFullName;
              data.merchantType = data.storeType;
              data.merchantAbbrName = data.storeAbbrName;
              data.merchantAccountType = data.storeAccountType;
              data.merchantAddress = data.storeAddress;
              data.merchantProvince = data.storeProvince;
              data.merchantProvinceName = data.storeProvinceName;
              data.merchantCity = data.storeCity;
              data.merchantCityName = data.storeCityName;
              data.merchantDistrict = data.storeDistrict;
              data.merchantDistrictName = data.storeDistrictName;
              data.mrcRegister = data.storeRegister;
              data.merchantNo = data.storeNo;
              data.mrcAccountAttach = data.storeAccountAttach;
              if (Number(data.storeNature) === 1) {
                mrcdetail(data.merchantId).then((v) => {
                  data.mrcAccountRates = v.data.mrcAccountRates;
                  data.mrcAccount = v.data.mrcAccount;
                  resolve(data);
                });
              } else {
                data.mrcAccountRates = data.storeAccountRates;
                data.mrcAccount = data.storeAccount;
                resolve(data);
              }
            })
            .catch((err) => {
              reject(err);
            });
        }
      });
    },
    setAutonomy(context, data) {
      console.log(data);
      context.commit("Autonomy", data);
    },
    getConfig(context, data) {
      return new Promise((resolve) => {
        queryRegisterConfig(data).then(({ data, code }) => {
          if (code === 200) {
            const list = data.list;
            context.commit("setConfig", list);
            resolve(list);
          }
        });
      });
    },
  },
};
export default common;
